package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.dto.QueryPageBeanDTO;
import com.itheima.health.pojo.entity.CheckGroup;
import com.itheima.health.pojo.entity.CheckItem;
import com.itheima.health.pojo.result.PageResult;
import com.itheima.health.pojo.result.Result;
import com.itheima.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;
    @GetMapping("/findPage")
    public Result findPage(QueryPageBeanDTO queryPageBean){
        PageResult page = checkGroupService.findPage(queryPageBean);
        return new Result(true, MessageConst.ACTION_SUCCESS, page);
    }

    @PostMapping("/add")
    public Result add(@RequestParam("checkitemIds") List<Integer> ids, @RequestBody CheckGroup checkGroup){
        checkGroupService.add(ids, checkGroup);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam("id") Integer id){
        CheckGroup res = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        List<Integer> ids = res.getCheckItems().stream().map(CheckItem::getId).collect(Collectors.toList());
        return new Result(true, MessageConst.ACTION_SUCCESS, ids);
    }

    @GetMapping("/findById")
    public Result findById(@RequestParam("id")Integer id){
        CheckGroup res = checkGroupService.findById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, res);
    }

    @PostMapping("/edit")
    public Result edit(@RequestParam("checkitemIds") List<Integer> ids, @RequestBody CheckGroup checkGroup){
        checkGroupService.edit(ids, checkGroup);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @PostMapping("/deleteCheckGroupitemById")
    public Result delById(@RequestParam("id") Integer id){
        checkGroupService.delById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        return new Result(true, MessageConst.ACTION_SUCCESS, checkGroupList);
    }
}
